package com.xuetang9.t9.hotel.services.impl;

import com.xuetang9.t9.hotel.beans.DiningTable;
import com.xuetang9.t9.hotel.beans.EConsumption;
import com.xuetang9.t9.hotel.mapper.IDiningTableMapper;
import com.xuetang9.t9.hotel.mapper.IEConsumptionMapper;
import com.xuetang9.t9.hotel.services.IDiningTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ProjectName: MyHotelProject
 * @Package: com.xuetang9.t9.hotel.service.impl
 * @ClassName: DiningTableService
 * @Author: ChinaHp
 * @Description: 餐桌服务层实现类
 * @Date: 2020/2/24 16:07
 * @Version: 1.0
 */
@Service
public class DiningTableService implements IDiningTableService {
    @Autowired
    private IDiningTableMapper mapper;
    @Autowired
    private IEConsumptionMapper ieConsumptionMapper;
    @Override
    public boolean add(DiningTable diningTable) {
        return mapper.insert(diningTable) > 0;
    }

    @Override
    public DiningTable toUp(int tableNum) {
        return mapper.select(tableNum);
    }

    @Override
    public boolean update(DiningTable diningTable) {
        return mapper.update(diningTable) > 0;
    }

    @Override
    public boolean del(int tableNum) {
        return mapper.delete(tableNum) > 0;
    }

    @Override
    public boolean delByTypeId(int typeId) {
        return mapper.delByTypeId(typeId) > 0;
    }

    @Override
    public int dataCount() {
        return mapper.dataSize();
    }

    @Override
    public List<DiningTable> queryById(int typeId) {
        return mapper.selectType(typeId);
    }

    @Override
    public List<DiningTable> queryAll() {
        return mapper.selectAll();
    }

    @Override
    public List<DiningTable> queryPageData(int currentPage, int size) {
        return mapper.selectLimit((currentPage-1)*size, size);
    }

    @Override
    public long pageSize(int size) {
        long dataSize = mapper.dataSize();
        return dataSize % size == 0 ? dataSize / size : dataSize / size + 1;
    }

    @Override
    public boolean changeDiningTable(Integer source, Integer destination) {
        DiningTable sourceTable = mapper.select(source);
        if(sourceTable == null || !sourceTable.getState().equals("就餐中")){
            return false;
        }
        DiningTable destinationTable = mapper.select(destination);
        if(destination == null || !destinationTable.getState().equals("空桌")){
            return false;
        }
        //进行换桌，首先将源桌位设置为“空桌”
        sourceTable.setState("空桌");
        mapper.update(sourceTable);


        //将就餐表中对应的就餐信息桌位改为目标桌位
        EConsumption consumption = ieConsumptionMapper.selectByTableId(sourceTable.getTableNum());
        consumption.setTablesId(destinationTable.getTableNum());
        ieConsumptionMapper.update(consumption);

        //将目标桌位设置为就餐中
        destinationTable.setState("就餐中");
        mapper.update(destinationTable);

        return false;
    }

    @Override
    public boolean cleanTable(Integer tableId) {
        if(tableId == null){
            return false;
        }

        //查找就餐列表填写清台信息
        EConsumption consumption = ieConsumptionMapper.selectByTableId(tableId);
        if(consumption != null){
            LocalDateTime localDateTime = LocalDateTime.now();
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(localDateTime.getYear()).append("-");
            stringBuffer.append(localDateTime.getMonthValue()).append("-");
            stringBuffer.append(localDateTime.getDayOfMonth()).append("-").append(" ");
            stringBuffer.append(localDateTime.getHour()).append(":");
            stringBuffer.append(localDateTime.getMinute()).append(":");
            stringBuffer.append(localDateTime.getSecond()).append(":");
            consumption.setComment(stringBuffer.toString()+"时间执行清台");
            consumption.setDiningstate(1);
            ieConsumptionMapper.update(consumption);
        }
        //查询桌位
        DiningTable diningTable = mapper.select(tableId);
        diningTable.setState("空桌");
        mapper.update(diningTable);
        return true;
    }

    @Override
    public long maxId() {
        return mapper.maxId();
    }

    @Override
    public List<DiningTable> selectByAllByType() {
        return mapper.selectByAllByTable();
    }
}
